﻿using System;
using System.Collections.Generic;
using Common;
using System.Web.Script.Serialization;
using Educational.Business;
using Basic.Business;
using Educational.VO;

public partial class Educational_Ajax_GradeManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //强制刷新页面
        Response.Buffer = true;
        Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
        Response.Expires = 0;
        Response.CacheControl = "no-cache";
        Response.AppendHeader("Pragma", "No-Cache"); 

        JavaScriptSerializer json = new JavaScriptSerializer();
        if (!String.IsNullOrEmpty(Request.QueryString["Action"]))
        {
            string action = BadChar.ReplaceBadChar(Request.QueryString["Action"]);
            switch (action)
            {
                #region deleteGrade删除年级
                case "deleteGrade":
                    {
                        Result<Object> result = new Result<Object>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            School school = (School)Session["school"];
                            int gradeId = int.Parse(Request.QueryString["gradeId"]);
                            GradeAdmin.DeleteGrade(school.schoolId, gradeId);
                            result.error = false;
                            result.msg = "删除年级成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                #region saveUpdateGrade保存年级修改
                case "saveUpdateGrade":
                    {
                        Result<Object> result = new Result<Object>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            School school = (School)Session["school"];
                            int gradeId = int.Parse(Request.Form["gradeId"]);
                            string gradeName = BadChar.ReplaceBadChar(Request.Form["gradeName"].Trim());
                            GradeAdmin.UpdateGrade(school.schoolId, gradeId, gradeName);
                            result.error = false;
                            result.msg = "修改年级成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                #region saveAddGrade保存年级添加
                case "saveAddGrade":
                    {
                        Result<Object> result = new Result<Object>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            School school = (School)Session["school"];
                            string gradeName = BadChar.ReplaceBadChar(Request.Form["gradeName"].Trim());
                            if (gradeName.Equals(""))
                                throw new Exception("年级名称不能为空");
                            GradeAdmin.AddGrade(school.schoolId, gradeName);
                            result.error = false;
                            result.msg = "添加年级成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                #region search查询列表
                case "search":
                    {
                        Result<Grade> result = new Result<Grade>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            int page = int.Parse(Request["page"]);
                            int perPage = int.Parse(Request["perpage"]);
                            School school = (School)Session["school"];
                            PageResult<Grade> pageResult = GradeAdmin.GetsGrade(school.schoolId, (page - 1) * perPage, page * perPage);
                            result.data = pageResult.data;
                            result.total = pageResult.totalCount;
                            result.page = page;
                            result.error = false;
                            result.msg = "查询年级列表成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                default:
                    {
                        Result<object> result = new Result<object>();
                        result.error = true;
                        result.msg = StringTable.ErrorUnknowAction;
                        Response.Write(json.Serialize(result));
                    }
                    break;
            }
        }
        else
        {
            Result<object> result = new Result<object>();
            result.error = true;
            result.msg = StringTable.ErrorMissingParameters;
            Response.Write(json.Serialize(result));
        }
        Response.End();
    }
}
